package razor.blade;

import javafx.beans.binding.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.stage.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.scene.shape.*;
import javafx.scene.control .*;
import tee.binding.*;
public class DialogX {
    private DialogX me;
    private Numeric x;
    private Numeric y;
    private Numeric width;
    //private Numeric innerWidth;
    private Numeric height;
    //private Numeric innerHeight;
    private It<Color>color;
    private Group root;
    private Group contentGroup;
    private Group backGroup;
    private Group upGroup;
    private Stage stage ;
    private Task onClose;
    public void close() {
        stage.close();
        }
    public void show(Stage p) {
        StageBuilder.create().icons(p.getIcons()).applyTo(stage);
        stage.show();
    }
    public DialogX() {
        me = this;
        //x = new Numeric().value(0);
        //y = new Numeric().value(0);
	x = new Numeric().value(0).afterChange(new Task() {
	    @Override public void doTask() {
		if (stage != null) {
		    stage.setX(x.value());
		}
	    }
	});
	//y = new SimpleDoubleProperty(0);
	y = new Numeric().value(0).afterChange(new Task() {
	    @Override public void doTask() {
		if (stage != null) {
		    stage.setY(y.value());
		}
	    }
	});
        color=new It<Color>().value(Color.web("#003366"));
        //me.width = new Numeric().value(600);
        //me.height = new Numeric().value(400);
	me.width = new Numeric().value(600).afterChange(new Task(){
	    @Override public void doTask() {
		if(stage!=null)
		stage.setWidth(me.width.value());
		//System.out.println(primaryStage);
	    }
	});
	me.height = new Numeric().value(400).afterChange(new Task(){
	    @Override public void doTask() {
		if(stage!=null)
		stage.setHeight(me.height.value());
		//System.out.println(primaryStage);
	    }
	});
        //innerWidth = new Numeric().value(600 - 32);
        //innerHeight = new Numeric().value(400 - 32);
        /*me.width.addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                innerWidth.set(me.width.get() - 32);
                stage.setWidth(me.width.get());
            }
        });
        me.height.addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                innerHeight.set(me.height.get() - 32);
                stage.setHeight(me.height.get());
            }
        });
        me.x.addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                stage.setX(me.x.get());
            }
        });
        me.y.addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                stage.setY(me.y.get());
            }
        });*/
        
        
        root = new Group();
        backGroup = new Group();
        contentGroup = new Group();
        root.getChildren().add(new Plate()
                .width(me.width.minus(32))
                .height(me.height.minus(32))
                .color(color)
                .x(16).y(16).node());
        root.getChildren().add(backGroup);
        Rectangle tow = new Rectangle();
        tow.widthProperty().bind(new Tee<Double>(me.width.minus(32)));
        tow.heightProperty().bind(new Tee<Double>(me.height.minus(32)));
        tow.setFill(Color.web("#00000001"));
	final Tow dragger=new Tow();
        root.getChildren().add(dragger
	/* {
            @Override public void onDrag(double x, double y) {
                me.x(me.x.value() + x);
                me.y(me.y.value() + y);
            }
        }*/.content(tow)
		//.x(16).y(16)
		.x(new Numeric().value(16).afterChange(new Task(){
                    @Override public void doTask() {
                        if(stage==null)return;
                        double dx=dragger.x().value()-16;
                        dragger.x(16);
                        me.x(me.x().value()+dx);
                    }
                }))
            .y(new Numeric().value(16).afterChange(new Task(){
                    @Override public void doTask() {
                        if(stage==null)return;
                        double dy=dragger.y().value()-16;
                        dragger.y(16);
                        me.y(me.y().value()+dy);
                    }
                }))
		.node());
        root.getChildren().add(contentGroup);
        upGroup = new Group();
        root.getChildren().add(upGroup);
        ImageView closeView = new ImageView();
        closeView.setImage(new Image(this.getClass().getResourceAsStream("steelBall18.png")));
        closeView.xProperty().bind(new Tee<Double>(width.minus(8+32)));
        closeView.setY(20);
        closeView.setCursor(Cursor.HAND);
        closeView.setOnMouseClicked(new EventHandler< MouseEvent>() {
            public void handle(MouseEvent event) {
                //onClose();
		if(onClose!=null)onClose.start();
            }
        });
        upGroup.getChildren().add(closeView);
        ImageView resizeView = new ImageView();
        resizeView.setImage(new Image(this.getClass().getResourceAsStream("steelBall18.png")));
        upGroup.getChildren().add(new Tow()/* {
                        @Override public void onDrag(double x, double y) {
                            //System.out.println("onDrag: "+me.width.get()+"/"+me.height.get());
                            me.width(me.width.value() + x);
                            me.height(me.height.value() + y);
                        }
                    }*/
                .x(me.width.minus(40))
                .y(me.height.minus(40))
                .cursor(Cursor.NW_RESIZE)
                .content(resizeView).node());
        stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.initModality(Modality.APPLICATION_MODAL);
        /*stage.xProperty().bindBidirectional(me.x);
        stage.yProperty().bindBidirectional(me.y);
        stage.widthProperty().bindBidirectional(me.width);
        stage.heightProperty().bindBidirectional(me.height);*/
        stage.setWidth(400);
        stage.setHeight(300);
        Scene scene = new Scene(root, Color.web("#00000000"));
        stage.setScene(scene);
    }
    public DialogX item(Node n) {
        contentGroup.getChildren().add(n);
        return this;
    }
    public DialogX background(Node n) {
        backGroup.getChildren().add(n);
        return this;
    }
    
    
    public DialogX onClose(Task it) {
        onClose=it;
	return this;
    }
    public DialogX x(double it) {
	x.value(it);
	return this;
    }
    public DialogX x(int it) {
	x.value(it);
	return this;
    }
    public Numeric x() {
	return x;
    }
    public DialogX x(It<Double> it) {
	x.bind(it);
	return this;
    }
    public DialogX y(double it) {
	y.value(it);
	return this;
    }
    public DialogX y(int it) {
	y.value(it);
	return this;
    }
    public Numeric y() {
	return y;
    }
    public DialogX y(It<Double> it) {
	y.bind(it);
	return this;
    }
    public DialogX width(double it) {
	width.value(it);
	return this;
    }
    public DialogX width(int it) {
	width.value(it);
	return this;
    }
    public Numeric width() {
	return width;
    }
    public DialogX width(It<Double> it) {
	width.bind(it);
	return this;
    }
    public DialogX height(double it) {
	height.value(it);
	return this;
    }
    public DialogX height(int it) {
	height.value(it);
	return this;
    }
    public Numeric height() {
	return height;
    }
    public DialogX height(It<Double> it) {
	height.bind(it);
	return this;
    }
     public DialogX color(Color it) {
	color.value(it);
	return this;
    }
    public DialogX color(It<Color> it) {
	color.bind(it);
	return this;
    }
}
